package com.xy.service.impl;

import java.util.List;
import java.util.Set;

import com.xy.dao.IBaseDao;
import com.xy.dao.impl.IBaseDaoImpl;
import com.xy.dao.impl.PageResults;
import com.xy.model.BJi;
import com.xy.model.BookFee;
import com.xy.model.Category;
import com.xy.model.Level;
import com.xy.model.Major;
import com.xy.model.Student;
import com.xy.service.StudentService;

public class StudentServiceImpl implements StudentService {

	  IBaseDao dao = new IBaseDaoImpl();
	  
	@Override
	public boolean add(Object object) {
		// TODO Auto-generated method stub
		try {
		      BJi bj = ((Student) object).getbJi();
		      Set<Student> stus = bj.getStudents();
		      stus.add((Student) object);
		      bj.setStudents(stus);
		      
//		      创建该学生的书费记录
		      BookFee bkf = new BookFee();
		      bkf.setPayFee(0);
		      bkf.setStudent((Student) object);
		      bkf.setbJi(bj);
		      
		      ((Student) object).getBookFees().add(bkf);
		      bj.getBookFees().add(bkf);
		      
		      dao.save(bkf);
		      dao.save(object);
		      dao.save(bj);
		      dao.commit();
		      return true;
		    } catch (Exception e) {
		      // TODO: handle exception
		      e.printStackTrace();
		      return false;
		    }
	}

	@Override
	public boolean update(Object object) {
		// TODO Auto-generated method stub
		try {
		      dao.update(object);
		      dao.commit();
		      return true;
		    } catch (Exception e) {
		      // TODO: handle exception
		      System.out.println(e.getMessage());
		      return false;
		    }
	}

	@Override
	public boolean delete(Object object) {
		// TODO Auto-generated method stub
		try {
		      dao.delete(object);
		      dao.commit();
		      return true;
		    } catch (Exception e) {
		      // TODO: handle exception
		      System.out.println(e.getMessage());
		      return false;
		    }
	}

	@Override
	  public <T> Object get(int id) {
	    // TODO Auto-generated method stub
	    return dao.get(Student.class, id);
	  }

	  @Override
	  public PageResults listByPage(int page) {
	    // TODO Auto-generated method stub
	    return null;
	  }

	  @Override
	  public <T> List<Object> list(Class<T> name) {
	    // TODO Auto-generated method stub
	    return (List<Object>) dao.getAll(name);
	  }

	  @Override
	  public PageResults query(Object... objects) {
	    // TODO Auto-generated method stub
	    String name = null;
	    int pageno = 0;
	    if (objects != null) {
	      name = (String) objects[0];
	      pageno = (Integer) objects[1];
	    }
	    return dao.getPageByFetchedHql("from Student where "+ "num like '%"+name+"%'"
	        ,
	        "select count(*) from Student where "+ "num like '%"+name+"%'"
	        , pageno+1, 10);
	  }
	  
	 @Override
	  public PageResults queryByC(Object... objects) {
	    // TODO Auto-generated method stub
	    Integer bj = null;
	    int pageno = 0;
	    if (objects != null) {
	      bj = Integer.valueOf((String) objects[0]);
	      pageno = (Integer) objects[1];
	    }
	    return dao.getPageByFetchedHql("from Student where "
	        + "bJi.id = "+bj+" "
	        ,
	        "select count(*) from Student where "
	    	        + "bJi.id = "+bj+" "
	        , pageno+1, 10);
	  }

	  public List<Student> getByObject(String propertyName, Object value) {
	    // TODO Auto-generated method stub
	    return dao.getListByProperty(Student.class, propertyName, value);
	  }
	  
	  public List<Student> queryByHql(String hql) {
		    // TODO Auto-generated method stub
		    return dao.getListByHQL(hql);
		  }
	  
	}
